package com.lh.java.connect;

import java.sql.SQLException;

import org.apache.log4j.Logger;

public class ReleasePoolHook extends Thread {
	public static final Logger logger = Logger.getLogger(ReleasePoolHook.class);
	ConnectionPool connpool;

	public ReleasePoolHook(ConnectionPool pool) {
		// register it
		connpool = pool;
		Runtime.getRuntime().addShutdownHook(new Thread(this));
		logger.info(">>> shutdown hook registered...");
	}

	@Override
	public void run() {
		// TODO Auto-generated method stub
		logger.info("\n>>> About to execute: "
				+ ReleasePoolHook.class.getName()
				+ ".run() to clean up before JVM exits.");
		this.cleanUp();
		logger.info(">>> Finished execution: "
				+ ReleasePoolHook.class.getName() + ".run()");
	}

	private void cleanUp() {
		if (connpool != null) {
			try {
				connpool.closeConnectionPool();
				logger.info("Pool realeased....");
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				logger.warn("Pool released with exception", e);
			}

		}
	}
}
